package com.java3_17;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class java3_17 {
    //    public static void main(String[] args) {
//        Collection<String> coll = new ArrayList<>();
//        coll.add("hello");
//        coll.add("java");
//        coll.add("world");
//        coll.add("love");
//        coll.add("you");
//        coll.add("mom");
//        coll.add("dad");
//        coll.add("noon");
//
//        for(String s : coll){
//            System.out.println(s);
//        }
//
//        System.out.println("------------------------");
//
//        coll.removeIf(new Predicate<String>() {
//            @Override
//            public boolean test(String s) {
//                StringBuilder stringBuilder = new StringBuilder();
//                stringBuilder.append(s);
//                StringBuilder reverse = stringBuilder.reverse();
//                return s.equals(reverse.toString());
//            }
//        });
//
//        Iterator<String> iterator = coll.iterator();
//        while(iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
//
//
//    }
//    public static void main(String[] args) {
////        //创建员工对象
////        Employee e1 = new Employee("zhangsan", 6000, 20);
////        Employee e2 = new Employee("lisi", 7000, 20);
////        Employee e3 = new Employee("wangwu", 6000, 24);
////        Employee e4 = new Employee("zhaoliu", 9000, 33);
////
////
////        Employee[] employees = {e1, e2, e3, e4};
////        Arrays.sort(employees);
////        for (int i = 0; i < employees.length; i++) {
////            Employee employee = employees[i];
////            System.out.println(employee.toString());
////        }
////
////        System.out.println("--------------------------");
////        Comparator<Employee> comparator = new Comparator<>() {
////            @Override
////            public int compare(Employee o1, Employee o2) {
////                if(o1.getAge() != o2.getAge()){
////                    return Integer.compare(o1.getAge(),o2.getAge());
////                }
////                return o1.getName().compareTo(o2.getName());
////
////            }
////        };
////
////        Arrays.sort(employees,comparator);
////        for (Employee e : employees){
////            System.out.println(e.toString());
////        }
////
////    }
//}
//    public static void main(String[] args) {
////        Coordinate<String> coordinate = new Coordinate<>("zhangsan","打篮球");
////        System.out.println(coordinate.toString());
////
////        Coordinate<Double> coordinate2 = new Coordinate<>(3.14,15.926);
////        System.out.println(coordinate2.toString());
//
////        Person<Person> my = new Person<>("zhangsan",new Person("lisi"));
////        System.out.println(my.toString());
//
////        Demon demon = new Demon("白娘子");
////        Person<Demon> demonPerson = new Person<>("许仙", demon);
////        System.out.println(demonPerson.toString());
//        //
//        //使用泛型可能是因为填入的数据类型未知
////        ArrayList<Integer> arrayInteger = new ArrayList<>();
////        arrayInteger.add(11);
////
////        Integer n = arrayInteger.get(0);// 这条代码底层如下：
////
////        //（1）get() 方法的返回值返回的是 Object 类型
////        Object object = arrayInteger.get(0);
////        //（2）编译器自动插入 Integer 的强制类型转换
////        Integer n = (Integer) object;
//
//
//
//    }
//    public static void main(String[] args) {
////        CopyOnWriteArrayList<Object> objects = new CopyOnWriteArrayList<>();
////        Collections.synchronizedMap()
////        new CopyOnWriteArraySet<>()
////        ConcurrentHashMap<Object, Object> objectObjectConcurrentHashMap = new ConcurrentHashMap<>();
//
////        Lock lock1 = new ReentrantLock(true); //公平锁
////        Lock lock2 = new ReentrantLock(false); //非公平锁
//
////        MyThread1 myThread1 = new MyThread1();
////        FutureTask futureTask = new FutureTask(myThread1);
////        Thread thread = new Thread(futureTask);
////        thread.start();
//
//
//        //只有当初始的Latch变成0之后await才会被唤醒
////        CountDownLatch countDownLatch = new CountDownLatch(5);
////        for (int i = 0; i < 5; i++) {
////            new Thread(() -> {
////                System.out.println(Thread.currentThread().getName() + "线程正在执行");
////                countDownLatch.countDown();
////            },String.valueOf(i)).start();
////        }
////
////        try {
////            countDownLatch.await();
////        } catch (InterruptedException e) {
////            throw new RuntimeException(e);
////        }
////        System.out.println("线程已经全部结束");
//
//        //所有线程会被阻塞在一个位置，直到全部就绪一起释放
////        CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() {
////            @Override
////            public void run() {
////                System.out.println("所有线程已经就绪");
////            }
////        });
////
////        for (int i = 0; i < 5; i++) {
////            new Thread(new Runnable() {
////                @Override
////                public void run() {
////                    System.out.println(Thread.currentThread().getName() + "已经就绪");
////                    try {
////                        cyclicBarrier.await();
////                    } catch (InterruptedException e) {
////                        throw new RuntimeException(e);
////                    } catch (BrokenBarrierException e) {
////                        throw new RuntimeException(e);
////                    }
////                }
////            }, String.valueOf(i)).start();
////        }
//
//
//    }
//    public static void main(String[] args) {
    //线程安全操作
    //List类
        /*
        （1）使用Vector
        （2）使用Collections里面的synchronizedXxx方法
        （3）使用CopyOnWriteArrayList
        * *///1
    //使用synchronized套
//        Vector<Object> list = new Vector<>();
//        list.add(123);
//        list.add("hello world");
//        list.add('A');
//        list.add(3.14);

//        ArrayList<String> list = new ArrayList<>();
//        list.add("hello world");
//        list.add("zhangsan");
//        List<String> list1 = Collections.synchronizedList(list);
//        list1.add("lisi");

    //一步到位
//        List list1 = Collections.synchronizedList(new ArrayList<>());

//        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
//        list.add(123);
//        list.add(456);
////        System.out.println(list);
//        //使用增强for循环
//        for (Integer integer : list) {
//            System.out.println(integer);
//        }

    //Set
//        Set set = Collections.synchronizedSet(new HashSet<>());
//        set.add(123);
//        set.add("hello world");
//        CopyOnWriteArraySet<Object> set = new CopyOnWriteArraySet<>();
//        set.add(123);
//        set.add("hello world");
//        for(Object object : set){
//            System.out.println(object);
    //Map
//        Map map = Collections.synchronizedMap(new HashMap<>());
//        map.put("zhangsan",20);
//        map.put("lisi",22);
//        System.out.println(map);
//        ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>();
//        map.put(123,"zhangsan");
//        System.out.println(map);
    //    }
//    public static void main(String[] args) {
//        //公平锁和不公平锁
//        //
////        Ticket ticket = new Ticket(30);
////
////        new Thread(() -> {
////            while (ticket.getTicket() > 0) {
////                ticket.delTicket();
////            }
////        }, "线程1").start();
////        new Thread(() -> {
////            while (ticket.getTicket() > 0) {
////                ticket.delTicket();
////            }
////        }, "线程2").start();
////        new Thread(() -> {
////            while (ticket.getTicket() > 0) {
////                ticket.delTicket();
////            }
////        }, "线程3").start();
//
//        //可重入锁和不可重入锁
////        Ticket ticket = new Ticket();
////        ticket.saleTicket();
//
//        //使用Callable创建线程
//
////        FutureTask futureTask = new FutureTask(new CallableThread());
////        Thread thread = new Thread(futureTask);
////        thread.start();
//
////        Thread thread = new Thread(new FutureTask(new CallableThread()));
////        thread.start();
//
////        FutureTask futureTask = new FutureTask(new CallableThread());
////        Thread thread = new Thread(futureTask);
////        thread.start();
////
////        try {
////            Object o = futureTask.get();
////        } catch (InterruptedException e) {
////            throw new RuntimeException(e);
////        } catch (ExecutionException e) {
////            throw new RuntimeException(e);
////        }
////        System.out.println("线程执行完毕");
//
//
//
//
//    }
    public static void main(String[] args) {
        //JUC辅助类
        //CountDownLatch

//        CountDownLatch countDownLatch = new CountDownLatch(5);
//
//        for (int i = 0; i < 5; i++) {
//            new Thread(() -> {
//                System.out.println(Thread.currentThread().getName() + "已经准备就绪");
//                countDownLatch.countDown();
//            },String.valueOf(i)).start();
//        }
//
//        try {
//            countDownLatch.await();
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        System.out.println(Thread.currentThread().getName()+" ");

        //CyclicBarrier
        //把想要的线程归拢到一处
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() {
            @Override
            public void run() {
                System.out.println("所有线程已经就绪");
            }
        });
        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "已经就绪");
                    try {
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } catch (BrokenBarrierException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName() + "开始执行任务");
                }
            }, String.valueOf(i)).start();
        }


    }

}